package com.zero.youxiao.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Dep;
import com.zero.youxiao.dao.DepDao;
import com.zero.youxiao.dto.DepDto;

@Service("depService")
@Transactional(rollbackFor = Exception.class)
public class DepService {

	@Inject
	private DepDao depDao;

	public ObjectNode searchDeps(Integer depNo, String depName, String depType)
			throws SmsRuntimeException {
		try {
			List<Dep> deps = depDao.getDepTree(depNo, depName, depType);
			Integer maxLevel = depDao.getMaxLevel();

			if (deps.size() == 0)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.AUTH_BAD_ACCESS, "搜索部门为空");

			Dep dep = deps.get(0);
			System.out.println(dep);

			ObjectNode depTreeObjectNode = JsonCreator.generateJsonObj();
			depTreeObjectNode = jsonTree(depTreeObjectNode, deps,
					dep.getDepId(), 0, maxLevel);

			ObjectNode respoNode = JsonCreator.generateJsonObj();
			respoNode.put("text", dep.getDepName());
			respoNode.put("type", "folder");
			respoNode.put("dep_id", dep.getDepId());
			respoNode.put("dep_parent_id", dep.getParentId());
			ObjectNode childrenNode = JsonCreator.generateJsonObj();
			childrenNode.putPOJO("children", depTreeObjectNode);
			respoNode.putPOJO("additionalParameters", childrenNode);

			return respoNode;
		} catch (SmsRuntimeException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public ObjectNode jsonTree(ObjectNode parentObject, List<Dep> deps,
			int parentId, int level, int maxLevel) {
		for (Dep depTreeNodeDto : deps) {
			if (depTreeNodeDto.getnLevel() == (level + 1)
					&& depTreeNodeDto.getParentId() == parentId) {
				ObjectNode depNode = JsonCreator.generateJsonObj();
				depNode.put("text", depTreeNodeDto.getDepName());
				depNode.put("type", "folder");
				depNode.put("dep_id", depTreeNodeDto.getDepId());
				depNode.put("dep_parent_id", depTreeNodeDto.getParentId());

				// lowest layer do not recursion
				if (depTreeNodeDto.getnLevel() == maxLevel) {
					depNode.put("type", "item");
				} else {
					ObjectNode childrenNode = JsonCreator.generateJsonObj();
					ObjectNode childNode = JsonCreator.generateJsonObj();

					childNode = jsonTree(childNode, deps,
							depTreeNodeDto.getDepId(),
							depTreeNodeDto.getnLevel(), maxLevel);
					childrenNode.putPOJO("children", childNode);
					depNode.putPOJO("additionalParameters", childrenNode);
				}
				parentObject
						.putPOJO("dep" + depTreeNodeDto.getDepId(), depNode);
			}
		}
		return parentObject;
	}

	public DepDto load(Integer depId) throws SmsRuntimeException {
		try {
			return depDao.loadDepInfo(depId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void updateDep(Integer depId, Integer depNo, String depName,
			String depType, String depPhone, String depTax, String depDesc,
			String depCreateDate) throws SmsRuntimeException {
		try {
			Dep dep = new Dep();
			dep.setDepId(depId);
			dep.setDepNo(depNo);
			dep.setDepName(depName);
			dep.setDepType(depType);
			dep.setPhone(depPhone);
			dep.setDepFax(depTax);
			dep.setDepDesc(depDesc);
			dep.setCreateDate(depCreateDate);
			depDao.updateDep(dep);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void addDep(Integer parentDepId, Integer depNo, String depName,
			String depType, String depPhone, String depTax, String depDesc,
			String depCreateDate) throws SmsRuntimeException {
		try {
			Dep dep = new Dep();
			dep.setDepNo(depNo);
			dep.setDepName(depName);
			dep.setDepType(depType);
			dep.setPhone(depPhone);
			dep.setDepFax(depTax);
			dep.setDepDesc(depDesc);
			dep.setCreateDate(depCreateDate);
			dep.setParentId(parentDepId);
			depDao.add(dep);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void deleteDep(Integer depId) throws SmsRuntimeException {
		try {
			depDao.deleteDep(depId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

}
